<template>
    <div>
        费用名称:<el-input style="width: 150px;" v-model="fymc" placeholder="请输入费用名称"></el-input>

        <el-button type="primary" icon="el-icon-search" @click="chaxun()"></el-button>
        <!-- <el-button type="primary" @click="">查询</el-button> -->
        <el-button type="primary" @click="tianjia()" style="margin-left:1000px;">添加</el-button>
        <!-- 添加处方费用 -->
        <el-dialog title="添加处方附加费用" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
            <span>费用名称:</span>
            <el-input v-model="extrachargesName" placeholder="请输入处方附加费名称" style="width: 300px;"></el-input><br><br>
            <span>费用金额:</span>
            <el-input v-model="fee" placeholder="请输入费用金额" style="width: 300px;" type="number"></el-input><br><br>
            <span>处方类型:</span>
            <el-select style="width: 300px;" v-model="value" placeholder="请选择处方类型">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
            </el-select>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="qd()">确 定</el-button>
            </span>
        </el-dialog>

·
        <el-table :data="tableData" style="width: 120%; margin-top: 30px;">
            <el-table-column prop="preId" label="序号">
            </el-table-column>
            <el-table-column prop="extrachargesName" label="处方附加费名称">
            </el-table-column>
            <el-table-column prop="fee" label="金额">
            </el-table-column>
            <el-table-column prop="type" label="处方类型">
                <template slot-scope="sc">
                    {{ sc.row.type==0?'中药处方':'西药处方' }}
                    <!-- <span v-if="sc.row.type == 0">中药处方</span>
                    <span v-else-if="sc.row.type == 1">西药处方</span> -->
                </template>
            </el-table-column>
            <el-table-column prop="chaozuo" label="操作">

                <template slot-scope="sc">
                    <el-button type="primary" @click="xiugai(sc.row)" style="width: 60px;">修改</el-button>
                    <!-- 表格的当前行 -->
                    <el-button type="danger" @click="shanchu(sc.row.preId)" style="width: 60px;">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 修改处方附加费 -->
        <el-dialog title="修改处方附加费" :visible.sync="dialogTableVisible" width="30%">
            <span>费用名称:</span>
            <el-input v-model="extrachargesName" placeholder="请输入处方附加费名称" style="width: 300px;"></el-input><br><br>
            <span>费用金额:</span>
            <el-input v-model="fee" placeholder="请输入金额" style="width: 300px;" type="number"></el-input><br><br>
            <span>处方类型:</span>
            <el-select style="width: 300px;" v-model="value" placeholder="请选择处方类型">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
            </el-select>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="queding()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import axios from 'axios'
import dayjs  from 'dayjs';
export default {
    name: "Prescription",
    data() {
        return {
            fymc: "",
            preId: "",
            extrachargesName: "",
            fee: "",
            tableData: [],
            formData: [],
            dialogVisible: false,
            dialogTableVisible: false,
            options: [{
                value: '0',
                label: '中药处方'
            },
            {
                value: '1',
                label: '西药处方'
            }
            ],
            value: '',
        }
    },
    methods: {
        handleClose(done) {
        },
        /**
         * 附加费修改
         */
        xiugai(e) {
            this.dialogTableVisible = true;
            this.extrachargesName = e.extrachargesName
            this.fee = e.fee
            this.preId = e.preId
      
            // if(e.type == 0){
            //     this.value = "中药处方"
            // }else{
            //     this.value = "西药处方"
            // }
            // console.log(e)
        
        },
        queding() {
            this.$confirm('确认修改？')
                .then(_ => {
                    let formData = new FormData();
                    formData.append("preId", this.preId);
                    formData.append("extrachargesName", this.extrachargesName);
                    formData.append("fee", this.fee);
                    formData.append("type",this.value );
                    // console.log(formData)
                    axios({
                        url: "http://127.0.0.1:8686/api/DzmHisPrescriptionExtrachargesxiugai",
                        method: "post",
                        data: formData,
                        // headers:{
                        //     'Content-Type':'application/x-www-form-urlencoded'
                        // }
                    }).then((re) => {
                        if (re.data) {
                            this.dialogTableVisible = false
                            alert("修改成功")
                            this.one()
                            console.log(this.fee)
                        } else {
                            alert("修改失败")
                        }
                    })
                        .catch(re => {

                            console.log(re)
                         });
                })
                .catch(_ => { });
        },
        /**
         * 查询附加费用名称
         */
        chaxun() {
            axios({
                url: "http://127.0.0.1:8686/api/DzmHisPrescriptionExtrachargeschaxun",
                method: "get",
                params: { extrachargesName: this.fymc }
            }).then((re) => {
                this.tableData = re.data
            })
        },
    
        /**
         * 添加附加费用
         */
        tianjia() {
            this.dialogVisible = true
            this.extrachargesName = ""
            this.fee = ""
            this.value = ""
        },
        //    确定
        qd() {1
            this.$confirm("确定添加")
                .then(_ => {
                    let formdata = new FormData()
                    formdata.append("extrachargesName", this.extrachargesName)
                    formdata.append("fee", this.fee)
                    formdata.append("type", this.value)

                    axios({
                        url: "http://127.0.0.1:8686/api/DzmHisPrescriptionExtrachargestianjia",
                        method: "post",
                        data: formdata
                    }).then((re) => {
                        if (re.data) {
                            this.dialogVisible = false
                            alert("添加成功")
                            this.one()
                        } else {
                            alert("添加失败")
                        }
                    })
                })
                .catch(_ => {

                });

        },
        /**
         * 附加费用删除
         */
        shanchu(preId) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {    
                    axios({
                        url: "http://127.0.0.1:8686/api/DzmHisPrescriptionExtrachargesshanchu",
                        method: "delete",
                        params: { preId: preId }
                    }).then((re) => {
                        if (re.data) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.one()
                        } else {
                            alert("删除失败")
                        }

                    })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
            let formData = new FormData();
            formData.append("preId", preId);
    
        },

        /**
         * 把初始化页面包起来
         */
        one() {
            axios({
                url: "http://127.0.0.1:8686/api/DzmHisPrescriptionExtrachargesxianshi",
                method: "get"
            }).then((re) => {
                this.tableData = re.data
            })
        }


    },
    mounted() {
        /**
         * 显示附加费用页面
         */
        this.one()
        // axios({
        //     url:"http://127.0.0.1:8686/api/DzmHisPrescriptionExtrachargesxianshi",
        //     method:"get" 
        // }).then((re)=>{
        //     for(let i=0;i<re.data.length;i++){
        //         let a=re.data[i];
        //         if (a.type == 0) {
        //             a.type= '中药处方'
        //         } else if(a.type == 1){
        //              a.type = '西药处方'
        //         }
        //             this.tableData = re.data
        //     }


        //     console.log(re.data)

        // })
    }
}
</script>

<style></style>